package com.wis.traffic.post.base;

import com.wis.traffic.post.util.PostUtil;
import com.wis.traffic.entity.base.BaseDAO;
import com.wis.traffic.common.common.DataBaseException;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class PostBaseDAOImpl<T,PK extends Serializable>  extends PostUtil implements BaseDAO<T, Serializable> {
    // sqlmap.xml定义文件中对应的sqlid  
    public static final String SQLID_INSERT = "insert";  
    public static final String SQLID_INSERT_BATCH = "insertBatch";
	public static final String SQLID_INSERT_ID = "insertBatchReturnId";
    public static final String SQLID_UPDATE = "update";  
    public static final String SQLID_UPDATE_PARAM = "updateParam";  
    public static final String SQLID_UPDATE_BATCH = "updateBatch";
    public static final String SQLID_UPDATE_PK = "updateByPrimaryKeySelective";
    public static final String SQLID_DELETE = "delete";  
    public static final String SQLID_DELETE_PARAM = "deleteParam";  
    public static final String SQLID_DELETE_BATCH = "deleteBatch";  
    public static final String SQLID_TRUNCATE = "truncate";  
    public static final String SQLID_SELECT = "select";  
    public static final String SQLID_SELECT_PK = "selectByPrimaryKey";  
    public static final String SQLID_SELECT_PARAM = "selectByParam";  
    public static final String SQLID_SELECT_BATCH = "selectBatch";
    public static final String SQLID_SELECT_NEARBY = "selectNearBy";
    public static final String SQLID_SELECT_CUSTOM = "selectCustom";
    public static final String SQLID_SELECT_FK = "selectFk";  
    public static final String SQLID_COUNT = "count";  
    public static final String SQLID_COUNT_PARAM = "countParam";
    public static final String SQLID_COUNT_CUSTOM = "countCustom";
    
	/**
	 * 新增记录
	 */
	public int insert(String nameSpace, T entity) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;
		try {
			rows = sqlSession.insert(nameSpace + "." + SQLID_INSERT, entity);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}

	public int insertBatchReturnId(String nameSpace, T entity) {
		SqlSession sqlSession = getSqlSession();
		int id = -1;
		try {
			id = sqlSession.insert(nameSpace + "." + SQLID_INSERT_ID, entity);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return id;
	}


	/**
	 * 根据主键修改对象
	 */
	public int update(String nameSpace, T entity) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;
		try {
			rows = sqlSession.update(nameSpace + "." + SQLID_UPDATE_PK, entity);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}

	public int updateParam(String nameSpace, Map param) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;
		try {
			rows = sqlSession.update(nameSpace + "." + SQLID_UPDATE_PARAM, param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}

	/**
	 * 根据主键删除对象信息
	 */
	public int delete(String nameSpace, String primaryKey) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;
		try {
			rows = sqlSession.delete(nameSpace + "." + SQLID_DELETE, primaryKey);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}

	public int deleteParam(String nameSpace, Map param) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;
		try {
			rows = sqlSession.delete(nameSpace + "." + SQLID_DELETE_PARAM, param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}

	public int truncate(String nameSpace) {
		SqlSession sqlSession = getSqlSession();
		int rows = 0;  
        try {  
            rows = sqlSession.delete(nameSpace + "." + SQLID_TRUNCATE);
			sqlSession.commit();
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
        } finally {
			sqlSession.close();
		}
        return rows; 
	}

	public int count(String nameSpace) {
		SqlSession sqlSession = getSqlSession();
		int result = 0;
		try {
			result = sqlSession.selectOne(nameSpace + "." + SQLID_COUNT);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return result;
	}

	public int count(String nameSpace, Object param) {
		SqlSession sqlSession = getSqlSession();
		int result = 0;
		try {
			result = sqlSession.selectOne(nameSpace + "." + SQLID_COUNT_PARAM, param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return result;
	}

	public T get(String nameSpace, Serializable primaryKey) {
		SqlSession sqlSession = getSqlSession();
		try {
			return sqlSession.selectOne(nameSpace + "." + SQLID_SELECT_PK, primaryKey);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
	}
	
	/**
	 * 根据主键查询对象详情
	 */
	public T getById(String nameSpace, String primaryKey) {
		SqlSession sqlSession = getSqlSession();
        try {
            return sqlSession.selectOne(nameSpace + "." + SQLID_SELECT_PK, primaryKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
        } finally {
			sqlSession.close();
		}
    }
	
	/**
	 * 根据主键查询对象详情，主键为int类型
	 */
	public T getById(String nameSpace, Integer primaryKey) {
		SqlSession sqlSession = getSqlSession();
        try {
            return sqlSession.selectOne(nameSpace + "." + SQLID_SELECT_PK, primaryKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
        } finally {
			sqlSession.close();
		}
    }

	public List<T> select(String nameSpace) {
		SqlSession sqlSession = getSqlSession();
		try {
			return sqlSession.selectList(nameSpace + "." + SQLID_SELECT);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
	}

	/**
	 * 根据指定条件查询对应的SQL语句
	 */
	public List<T> selectParam(String nameSpace, Map param) {
		SqlSession sqlSession = getSqlSession();
		try {
			return sqlSession.selectList(nameSpace + "." + SQLID_SELECT_PARAM, param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
	}

	/**
     * 根据sqlid和条件查询信息
     * @return
     */
    public List<Object> getObject(String nameSpace, String selectSqlId, Map<String, Object> param){
		SqlSession sqlSession = getSqlSession();
        try {
            return sqlSession.selectList(nameSpace + "." + selectSqlId, param);
        } catch (Exception e) {
            e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
        } finally {
			sqlSession.close();
		}
    }
    
    /**
     * 根据条件查询信息数量
     * @param nameSpace
     * @param selectSqlId
     * @return
     */
    public int getObjectCount(String nameSpace, String selectSqlId, Map<String, Object> param) {
		SqlSession sqlSession = getSqlSession();
    	try {
            return sqlSession.selectOne(nameSpace + "." + selectSqlId, param);
        } catch (Exception e) {
            e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
        } finally {
			sqlSession.close();
		}
    }
    
	/**
     * 根据sqlid和param插入
     * @param selectSqlId
     * @param param
     * @return
     */
    public int insertObject(String nameSpace, String selectSqlId, Map<String, Object> param){
		SqlSession sqlSession = getSqlSession();
        int rows = 0;
		try {
			rows = sqlSession.insert(nameSpace + "." + selectSqlId, param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
    }

    /**
     * 修改方法
     * @param nameSpace
     * @param selectSqlId
     * @param param
     * @return
     */
    public int updateObject(String nameSpace, String selectSqlId, Map<String, Object> param) {
		SqlSession sqlSession = getSqlSession();
    	int rows = 0;
		try {
			rows = sqlSession.update(nameSpace + "." + selectSqlId, param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
    }
    
    /**
     * 删除数据
     * @param nameSpace
     * @param selectSqlId
     * @param param
     * @return
     */
    public int deleteObject(String nameSpace, String selectSqlId, Map<String, Object> param) {
		SqlSession sqlSession = getSqlSession();
    	int rows = 0;
		try {
			rows = sqlSession.delete(nameSpace + "." + selectSqlId, param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
    }

    /**
     * 批量插入数据
     * @param nameSpace
     * @param commentList
     * @return
     */
    public int insertBatch(String nameSpace,List<Object> commentList){
		SqlSession sqlSession = getSqlSession();
    	int rows = 0 ;	//返回的插入次数
    	try {
			rows = getSqlSession().insert(nameSpace + "." +  SQLID_INSERT_BATCH,commentList);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
    	return rows;
    }


	/**
	 * 批量修改数据
	 * @param nameSpace
	 * @param param
	 * @return
	 */
	public int updateBatch(String nameSpace,Map<String, Object> param){
		SqlSession sqlSession = getSqlSession();
		int rows = 0 ;	//返回的修改次数
		try {
			rows = sqlSession.insert(nameSpace + "." +  SQLID_UPDATE_BATCH,param);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataBaseException("数据库操作异常。异常内容：" + e.getMessage());
		} finally {
			sqlSession.close();
		}
		return rows;
	}
}
